//
//  DBManager.m
//  Mango
//
//  Created by lanou3g on 16/5/14.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "DBManager.h"
#import <FMDatabase.h>
#import "XJUserInfoManager.h"
@implementation DBManager
+ (instancetype)shareManager {
    static DBManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[DBManager alloc]init];
    });
    return manager;
}

//数据库指针
static FMDatabase *database = nil;

//打开数据库
- (void)openDB {
    NSString *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *dbPath = [documentPath stringByAppendingPathComponent:@"Mango.sqlite"];
//    NSLog(@"%@",dbPath);
    //打开创建数据库
    database = [FMDatabase databaseWithPath:dbPath];
    if ([database open]) {
//        NSLog(@"数据库打开成功");
    }
}

/**
 *  创建表
 */
- (void)createTable {
    
    NSString *sql = @"CREATE TABLE IF NOT EXISTS table_history (searchid INTEGER PRIMARY KEY AUTOINCREMENT,searchHistory TEXT)";
    BOOL result = [database executeUpdate:sql];
    if (result) {
//        NSLog(@"创建表成功");
    }
}

- (void)createCollectTable {
    NSString *collectSql = @"CREATE TABLE IF NOT EXISTS table_collect (collectid INTEGER PRIMARY KEY AUTOINCREMENT,uid TEXT,contentid INT,title TEXT,pic TEXT)";
    BOOL result = [database executeUpdate:collectSql];
    if (result) {
//        NSLog(@"创建表成功");
    }
}

/**
 *  插入数据
 */
- (void)saveSearchHistory:(NSString *)history {
    [self openDB];
    [self createTable];
    BOOL result = [database executeUpdateWithFormat:@"INSERT INTO table_history (searchHistory) VALUES (%@)",history];
    if (result) {
//        NSLog(@"插入数据成功");
    }
}

- (void)collectLeftModel:(BaseModel *)model {
    [self openDB];
    [self createCollectTable];
    BOOL result = [database executeUpdateWithFormat:@"INSERT INTO table_collect (uid,contentid,title,pic) VALUES (%@,%ld,%@,%@)",[XJUserInfoManager shareManager].uid,model.ID,model.title,model.pic];
    if (result) {
//        NSLog(@"插入数据成功");
    }
}

/**
 *  移除数据
 */
- (void)deleteSearchHistory:(NSString *)history {
    [self openDB];
    [self createTable];
    BOOL result = [database executeUpdateWithFormat:@"DELETE FROM table_history WHERE searchHistory = %@",history];
    if (result) {
//        NSLog(@"删除成功");
    }
}

/**
 *  移除数据
 */
- (void)cancelCollectArticle:(NSInteger)contentID {
    [self openDB];
    [self createCollectTable];
    BOOL result = [database executeUpdateWithFormat:@"DELETE FROM table_collect WHERE contentid = %ld",contentID];
    if (result) {
//        NSLog(@"删除成功");
    }
}

/**
 *  查询搜索历史
 */
- (NSArray *)selectAllSearchHistory {
    [self openDB];
    [self createTable];
    FMResultSet *resultSet = [database executeQueryWithFormat:@"SELECT * FROM table_history"];
    //声明一个数组
    NSMutableArray *array = [NSMutableArray new];
    while ([resultSet next]) {
        NSString *history = [resultSet stringForColumn:@"searchHistory"];
        [array addObject:history];
    }
    return array;
}

//查询所有收藏内容
- (NSArray *)selecteAllCollect {
    [self openDB];
    [self createCollectTable];
    FMResultSet *resultSet = [database executeQueryWithFormat:@"SELECT * FROM table_collect WHERE uid = %@",[XJUserInfoManager shareManager].uid];
    //声明一个数组
    NSMutableArray *array = [NSMutableArray new];
    while ([resultSet next]) {
        BaseModel *model = [BaseModel new];
        //赋值
        model.ID = [resultSet intForColumn:@"contentid"];
        model.title = [resultSet stringForColumn:@"title"];
        model.pic = [resultSet stringForColumn:@"pic"];
        [array addObject:model];
    }
    return array;
}

//查看是否已收藏
- (BOOL)hasSave:(NSString *)searchHistory {
    [self openDB];
    [self createTable];
    BOOL hasSave = NO;
    FMResultSet *result = [database executeQueryWithFormat:@"SELECT * FROM table_history WHERE searchHistory = %@",searchHistory];
    while ([result next]) {
        hasSave = YES;
    }
    return hasSave;
}

- (BOOL)hasCollect:(NSInteger)contentID {
    [self openDB];
    [self createCollectTable];
    BOOL hasCollect = NO;
    FMResultSet *result = [database executeQueryWithFormat:@"SELECT * FROM table_collect WHERE uid = %@ AND contentid = %ld",[XJUserInfoManager shareManager].uid,contentID];
    while ([result next]) {
        hasCollect = YES;
    }
    return hasCollect;
}
@end
